package com.cms.action;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.cms.base.Pagination;
import com.cms.entity.Admin;
import com.cms.entity.Article;
import com.cms.entity.Channel;
import com.cms.entity.ChannelArticleMapper;
import com.cms.entity.Topic;
import com.cms.enums.ArticleTypeEnum;
import com.cms.service.ArticleService;
import com.cms.service.ChannelService;
import com.cms.service.TopicService;
import com.cms.util.CommonUtils;
import com.opensymphony.xwork2.ActionSupport;

public class ArticleAction extends ActionSupport {

	private int firstResult = 0;			
	private int maxResults = 1;		
	
	private ArticleService articleService;
	private ChannelService channelService;
	private TopicService topicService;
	//private AttachmentService attachementService;
	//private CommentService commentService;
	
	private int id;
	private Article article;
	private Channel channel;
	
	private Pagination<Article> pagination;
	private List<Channel> channelList;
	private List<Topic> topicList;
	//private List<Attachment> attachmentList;
	//private List<Comment> commentList;
	private List<Serializable> channelIds;
	private ArticleTypeEnum[] articleTypeEnum;
	
	
	@Override
	public String execute(){
		String offset =  ServletActionContext.getRequest().getParameter("pager.offset");
		if(CommonUtils.isNotEmpty(offset))
			firstResult = Integer.parseInt(offset);
		pagination = articleService.queryArticles(article, firstResult, maxResults);
		return SUCCESS;
	}

	public String toSave(){
		channelList = channelService.queryChannels(channel);
		articleTypeEnum = ArticleTypeEnum.values();
		topicList = topicService.queryTopics(null);
		return "toSave";
	}
	
	public String save(){
		Date date = new Date();
		article.setCreateTime(date);
		article.setUpdateTime(date);
		
		int id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("admin");
		Admin admin = new Admin();
		admin.setId(id);
		article.setAdmin(admin);
		article.setIsHeadline(Boolean.TRUE);
		article.setIsRecommend(Boolean.TRUE);

		articleService.save(article, channelIds);
		
		return "save";
	}

	public String toUpdate(){
		article = articleService.get(id);
		channelList = channelService.queryChannels(channel);
		return "toUpdate";
	}
	
	public String update(){
		articleService.update(article);
		return "update";
	}
	
	public String delete(){		
		articleService.delete(id);
		return "delete";
	}
	
	public String get(){
		article = articleService.get(id);
		return "get";
	}

	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}


	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Pagination<Article> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Article> pagination) {
		this.pagination = pagination;
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public List<Channel> getChannelList() {
		return channelList;
	}

	public void setChannelList(List<Channel> channelList) {
		this.channelList = channelList;
	}

	public List<Serializable> getChannelIds() {
		return channelIds;
	}

	public void setChannelIds(List<Serializable> channelIds) {
		this.channelIds = channelIds;
	}

	public ArticleTypeEnum[] getArticleTypeEnum() {
		return articleTypeEnum;
	}

	public void setArticleTypeEnum(ArticleTypeEnum[] articleTypeEnum) {
		this.articleTypeEnum = articleTypeEnum;
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public List<Topic> getTopicList() {
		return topicList;
	}

	public void setTopicList(List<Topic> topicList) {
		this.topicList = topicList;
	}
	
	
	
	
}
